<template>
  <el-form :model="shop" :rules="rules" ref="addShops" id="form">
    <el-form-item label="店铺名称" prop="shopName">
      <el-input v-model="shop.shopName" />
    </el-form-item>
    <el-form-item label="详细地址" placeholder="请输入地址" prop="address">
      <el-input v-model="shop.address" />
    </el-form-item>
    <el-form-item label="联系电话"  prop="phoneNumber">
      <el-input v-model="shop.phoneNumber" />
    </el-form-item>
    <el-form-item label="店铺简介">
      <el-input v-model="shop.intro" />
    </el-form-item>
    <!-- 时间选择器 -->
    <el-form-item label="营业时间">
      <el-select v-model="shop.time" placeholder="起始时间" margin-right="10px">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-select v-model="value" placeholder="结束时间">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <!-- 店铺照片 -->
    <el-form-item label="上传店铺头像" prop="icon">
      <el-upload ref="uploadRef" action="/api/upload" list-type="picture-card" :auto-upload="false" name="file" :on-success="uploadSuccess" :before-upload="beforeUpload" accept=".jpg,.png,.gif">
      <el-icon @click="doUpload">
        <Plus />
      </el-icon>
    </el-upload>
    <img :src="shop.shopicon" width="100" alt="">
    <el-button @click="doUpload" type="primary">点击上传</el-button>
    </el-form-item>
    <!-- 营业执照 -->
    <el-form-item label="上传营业执照" prop="icon">
      <el-upload ref="uploadRef" action="/api/upload" list-type="picture-card" :auto-upload="false" name="file" :on-success="uploadSuccess" :before-upload="beforeUpload" accept=".jpg,.png,.gif">
      <el-icon @click="doUpload">
        <Plus />
      </el-icon>
    </el-upload>
    <img :src="shop.openicon" width="100" alt="">
    <el-button @click="doUpload" type="primary">点击上传</el-button>
    </el-form-item>
    <!-- 优惠活动 -->
    <el-form-item label="优惠活动">
      <el-select v-model="value" margin-right="10px">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <!-- 新增重置 -->
    <el-form-item>
        <el-button type="primary" @click="onSubmit">新增</el-button>
        <el-button @click="clear">重置</el-button>
      </el-form-item>
  </el-form>
</template>

<script>
import { addShops } from '@/api/AddShops'
export default {
  name: 'AddShops',
  props: {
    cates: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      rules: {
        shopName: [
          {
            required: true,
            message: '店铺名称必填'
          }
        ],
        address: [
          {
            required: true,
            message: '详细地址必填'
          }
        ],
        phoneNumber: [
          {
            required: true,
            message: '联系电话必填'
          },
          {
            pattern: /^0?1[3|4|5|8][0-9]\d{8}$/,
            message: '请输入正确的联系电话'
          }
        ]
      },
      shop: {
        shopName: '',
        address: '',
        phoneNumber: '',
        intro: '',
        shopicon: '',
        openicon: '',
        time: ''
      },
      options: [
        {
          value: '06:00',
          label: '06:00'
        },
        {
          value: '07:00',
          label: '07:00'
        }
      ]
    }
  },
  watch: {
    cates: {
      handler (newValue) {
        this.shop = {
          ...newValue
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    clear () {
      this.$refs.addShops.resetFields()
    },
    add () {
    //  方法一
      this.$refs.addShops.validate().then(() => {
        addShops(this.shop).then(res => {
          if (res.data.code === 200) {
            this.$message({
              type: 'success',
              message: res.data.msg,
              duration: 2000,
              onClose: () => {
                this.$router.go(0)
              }
            })
          }
        })
        this.$refs.addShops.resetFields()
      }).catch(() => {
        alert('验证失败')
      })
    },
    // 图片上传
    beforeUpload (file) {
      if (file.size > 1024 * 1024 * 5) {
        alert('图片过大，不能超过5m')
        return false
      } else {
        return true
      }
    },
    doUpload () {
      this.$refs.uploadRef.submit()
    },
    uploadSuccess (res) {
      if (res.code === 200) {
        this.menu.icon = res.data.url
        this.$message({
          type: 'success',
          message: res.msg
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
#form {
  width: 600px;
  margin: 20px auto;

  .el-form-item {
    font-weight: bold;
    // height: 45px;

    .el-input {
      height: 45px;
      position: relative;
      top: -5px
    }
  }
}
</style>
